class CreateReplicas < ActiveRecord::Migration
  def self.up
    create_table :replicas do |t|
      t.column :crud,             :string,                 :default => "", :null => false
      t.column :for_id,           :string,   :limit => 36
      t.column :for_machine_id,   :string,   :limit => 36
      t.column :method_id,        :string,   :limit => 36,                 :null => false
      t.column :method_type,      :string,   :limit => 45, :default => "", :null => false
      t.column :deleted_id,       :string,   :limit => 36
      t.column :created_at,       :timestamp,                              :null => false
      t.column :updated_at,       :timestamp,                              :null => false
    end
    
    create_table :remote_clients do |t|
      t.column :client_id,        :string,   :limit => 36,                 :null => false
      t.column :guid,             :string,   :limit => 36,                 :null => false
    end

    create_table :remote_machines do |t|
      t.column :remote_client_id, :integer,                                :null => false
      t.column :machine_id,       :string,   :limit => 36,                 :null => false
      t.column :last_synced,      :integer,                 :default => -1
    end

    create_table :sync_settings do |t|
      t.column :protocol,           :string, :limit => 6,   :default => 'http'
      t.column :hostname,           :string, :limit => 200, :default => 'localhost'
      t.column :port,               :string, :limit => 6,   :default => '3000'
      t.column :down_uri,           :string, :limit => 150, :default => '/syncs/down.yaml'
      t.column :up_uri,             :string, :limit => 150, :default => '/syncs/up.yaml'
      t.column :handshake_uri,      :string, :limit => 150, :default => '/syncs/handshake.yaml'
      t.column :dump_uri,           :string, :limit => 150, :default => '/syncs/dump.yaml'
      t.column :proxy_host,         :string, :limit => 150
      t.column :proxy_port,         :string, :limit => 5
      t.column :proxy_user,         :string, :limit => 50
      t.column :proxy_pass,         :string, :limit => 50
      t.column :auth_user,          :string, :limit => 50
      t.column :auth_pass,          :string, :limit => 50
      t.column :batch_limit,        :integer,               :default => 80
      t.column :win_svn_username,   :string, :limit => 20,  :default => ''
      t.column :win_svn_password,   :string, :limit => 20,  :default => ''
      t.column :log_file,           :string, :limit => 20,  :default => 'log/replica.log'
      t.column :wipe_log,           :boolean,               :default => true
      t.column :unique_machine_id,  :string, :limit => 36,                  :null => false
      t.column :last_synced,        :integer,               :default => -1
      t.column :first_login,        :datetime,              :default => nil
    end

    # Example of User table with the required:
    #   UUID :id, UUID :guid, :last_synced timestamp and trackable columns
    # create_table :users, :id => false do |t|
    #   t.column :id, 'varchar(36) primary key', :null => false
    #   t.column :fullname, :string
    #   t.column :username, :string
    #   t.column :password_salt, :string
    #   t.column :password_hash, :string
    #   t.column :guid,          :string, :limit => 36, :null => false
    #   t.column :last_synced,   :integer
    #   t.column :created_by, :string, :limit => 36
    #   t.column :updated_by, :string, :limit => 36
    #   t.column :created_at, :timestamp
    #   t.column :updated_at, :timestamp
    # end
    
    # now that the tables exists, this should work
    $SYNC_CONFIG = ActiveRecord::Base.load_config
  end
  
  def self.down
    # drop_table :users
    drop_table :remote_clients
    drop_table :replicas
    drop_table :settings
    drop_table :remote_machines
  end
end
